package com.liwenfeng.file;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class EncodingDecodingTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "a我m";
        //编码
        byte[] bytes = s.getBytes();
        byte[] gbks = s.getBytes("gbk");
        System.out.println(Arrays.toString(bytes)); //[97, -26, -120, -111, 109]
        System.out.println(Arrays.toString(gbks)); //[97, -50, -46, 109]

        //解码
        String s1 = new String(bytes);//用utf-8解码utf-8的编码
        String s2 = new String(gbks);//用utf-8解码gbk的编码
        String s3 = new String(bytes, "gbk");//用gbk解码utf-8的编码
        String s4 = new String(gbks, "gbk");//用gbk解码gbk的编码
        System.out.println(s1); //a我m
        System.out.println(s2); //a��m（乱码）
        System.out.println(s3); //a鎴憁（乱码）
        System.out.println(s4); //a我m
    }
}
